package com.ecoolex.hotel.saas.platform.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.annotation.PlatformAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.platform.PlatformMemberAuthority;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.hotel.saas.entity.SalesUser;
import com.ecoolex.hotel.saas.entity.SalesUserWallet;
import com.ecoolex.hotel.saas.entity.SalesUserWalletWithdrawalApply;
import com.ecoolex.hotel.saas.enums.SalesUserWalletWithdrawalApplyStatus;
import com.ecoolex.hotel.saas.platform.core.PlatformConstant;
import com.ecoolex.hotel.saas.pojo.QuerySalesUserWalletWithdrawalApplyDTO;
import com.ecoolex.hotel.saas.ws.SalesUserService;
import com.ecoolex.hotel.saas.ws.StoreService;

@GraphQL2Module(type = Type.Platform, description = "用户钱包提现管理")
@PlatformAuthoritativeRegister(parentKey = PlatformConstant.AUTH_PROJECT_KEY,
	key = SalesUserWalletWithdrawalApplyGraphql.AUTH,
	name = "用户钱包提现管理",
	remark = "用户钱包提现管理")
public class SalesUserWalletWithdrawalApplyGraphql {

	public static final String AUTH = PlatformConstant.AUTH_PROJECT_KEY + "-SALES-USER-WALLET-WITHDRAWAL-APPLY";
	
	private static final String AUTH_AGREE = AUTH + "-AGREE";
	
	private static final String AUTH_REFUSE = AUTH + "-REFUSE";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@Reference
	private SalesUserService salesUserService;
	
	@Reference
	private StoreService storeService;
	
	@TypeField(type = SalesUserWalletWithdrawalApply.class, field = "salesUserWallet", description = "用户钱包")
	public SalesUserWallet salesUserWallet(@Source SalesUserWalletWithdrawalApply source) {
		return salesUserService.getSalesUserWallet(source.getWalletId());
	}
	
	@TypeField(type = SalesUserWallet.class, field = "salesUser", description = "用户")
	public SalesUser salesUser(@Source SalesUserWallet source) {
		return salesUserService.getByUserIdAndStoreId(source.getUserId(), source.getStoreId());
	}
	
	@TypeField(type = SalesUserWalletWithdrawalApply.class, field = "canAudit", description = "能审核")
	public Boolean canAudit(@Source SalesUserWalletWithdrawalApply source) {
		return SalesUserWalletWithdrawalApplyStatus.canAudit(source.getStatus());
	}
	
	@QueryField(field = "salesUserWalletWithdrawalApply", description = "用户钱包提现申请详情")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "用户钱包提现申请详情")
	public SalesUserWalletWithdrawalApply get(@NonNull Integer applyId, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return salesUserService.getSalesUserWalletWithdrawalApply(applyId);
	}
	
	@QueryField(field = "salesUserWalletWithdrawalApplyList", description = "用户钱包提现申请列表")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "用户钱包提现申请列表")
	public Pagination<SalesUserWalletWithdrawalApply> query(@NonNull QuerySalesUserWalletWithdrawalApplyDTO query, @NonNull Page page, PlatformMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return salesUserService.query(query, page);
	}
	
	@MutationField(field = "agreeSalesUserWalletWithdrawalApply", description = "同意用户提现申请")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_AGREE, name = "同意用户提现申请")
	public SalesUserWalletWithdrawalApply agree(@NonNull Integer applyId, String remark, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_AGREE);
		SalesUserWalletWithdrawalApply apply = salesUserService.getSalesUserWalletWithdrawalApply(applyId);
		Assert.isTrue(Check.notNull(apply),ResultCode.VERIFY.build(1), "申请记录不存在");
		apply.setRemark(remark);
		return salesUserService.agree(apply, user.userName());
	}
	
	@MutationField(field = "refuseSalesUserWalletWithdrawalApply", description = "拒绝用户提现申请")
	@PlatformAuthoritativeRegister(parentKey = AUTH, key = AUTH_REFUSE, name = "拒绝用户提现申请")
	public SalesUserWalletWithdrawalApply refuse(@NonNull Integer applyId,String refuseReason, String remark, PlatformMemberAuthority user) {
		user.assertAuth(AUTH_REFUSE);
		SalesUserWalletWithdrawalApply apply = salesUserService.getSalesUserWalletWithdrawalApply(applyId);
		Assert.isTrue(Check.notNull(apply),ResultCode.VERIFY.build(1), "申请记录不存在");
		apply.setRefuseReason(refuseReason);
		apply.setRemark(remark);
		return salesUserService.refuse(apply, user.userName());
	}
}
